package com.cade.agriculturemall.view.personalCenter.ordersManage;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;

import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.base.BaseFragmentAdapter;
import com.cade.agriculturemall.model.OrderEntity;
import com.cade.agriculturemall.model.ShoppingCarsEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by hshunxin on 2018/1/29.
 */

public class MyOrdersActivity extends BaseActivity<MyOrdersContract.IPresenter> implements MyOrdersContract.IView {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private int viewType;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_orders);
        setupToolbar(true);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void setupView() {
        viewPager = (ViewPager) findViewById(R.id.my_order_viewpager);
        tabLayout = (TabLayout) findViewById(R.id.my_order_tab_layout);
        initSelect();
        initViewPager();
        viewType = getIntent().getIntExtra("type", 0);
        setPager(viewType);
    }

    private List<String> mTitles = new ArrayList<String>() {
        {
            add("全部订单");
            add("待支付");
            add("待发货");
            add("待收货");
            add("待评价");
        }
    };

    /**
     * 初始化水平选择栏
     */
    private void initSelect() {
        //设置TabLayout的模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        //需要自定义布局时，需要在绑定viewpager前加入布局
        for (int i = 0; i < mTitles.size(); i++) {
            tabLayout.addTab(tabLayout.newTab().setText(mTitles.get(i)));
        }
    }

    @Override
    public void setupEvent() {

    }

    @Override
    public void setupData() {
    }

    @Override
    public MyOrdersContract.IPresenter setupPresenter() {
        return new MyOrdersPresenter(this);
    }

    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    /**
     * 初始化ViewPager
     */
    private void initViewPager() {
        MerchandiseListBaseFragment merchandiseListAll = MerchandiseListBaseFragment.
                newInstance(0);
        MerchandiseListBaseFragment merchandiseListDisPatch = MerchandiseListBaseFragment.
                newInstance(1);
        MerchandiseListBaseFragment merchandiseListReceive = MerchandiseListBaseFragment.
                newInstance(2);
        MerchandiseListBaseFragment merchandiseListFinish = MerchandiseListBaseFragment.
                newInstance(3);
        MerchandiseListBaseFragment merchandiseListWaitPay = MerchandiseListBaseFragment.
                newInstance(4);
        fragmentList.add(merchandiseListAll);
        fragmentList.add(merchandiseListWaitPay);
        fragmentList.add(merchandiseListDisPatch);//
        fragmentList.add(merchandiseListReceive);//
        fragmentList.add(merchandiseListFinish);//
        FragmentPagerAdapter fragmentAdapter = new BaseFragmentAdapter(getSupportFragmentManager(), fragmentList, mTitles);
        viewPager.setAdapter(fragmentAdapter);
//        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(4);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
//                ((MerchandiseListBaseFragment) fragmentList.get(tab.getPosition())).fresh();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    public void setPager(int viewType) {
        if (!TextUtils.isEmpty(viewType + "")) {
            viewPager.setCurrentItem(viewType);
        }
    }

    @Override
    public void onDownloadEntity(HashMap<OrderEntity, List<ShoppingCarsEntity>> hashMap) {

    }

    @Override
    public void doViewRefresh(OrderEntity orderEntity) {

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void deleteSuccess() {

    }

    @Override
    public void deleteFail() {

    }
}
